<?php
namespace app\index\controller;

use lib\wx\Liteapp;
use think\Db;
class Login extends Home
{
    public function login()
    {
        $data = $this->request->param();
        $wxapp_id      = $this->request->header('wxapp-id');
        $code = isset($data['code'])?$data['code']:'';
        if(!$code) $this->returnJson(-10001,"code参数有误",[],$data);

        $Liteapp = new Liteapp();
        $weappArr = $Liteapp->getSessionkey($code);
        $openid = $weappArr['openid'];
		$session_key = $weappArr['session_key'];
        if(!$openid) $this->returnJson(-10001,"openid获取失败，登陆异常",[],$data);
        
        $user = Db::name('wx_user')->where('openid',$openid)->find();
        $ip = $this->request->ip();
        $is_reg = 0;
        if(empty($user)){
            $insertData = [
                'openid'=>$openid,
                'addtime'=>time(),
                'wxapp_id'=>$wxapp_id,
                'last_login_time'=>time(),
                'last_logon_ip'=>$ip
            ];
            $user_id = Db::name('wx_user')->insertGetId($insertData);
            $is_reg = 1;
        }else{
            $upData = [
                'last_login_time'=>time(),
                'last_logon_op'=>$ip,
                'mdtime'=>time()
            ];
            $res = Db::name('wx_user')->where('openid',$openid)->update($upData);
            $user_id = $user['id'];
        }
        if($user_id > 0){

            $wxapp = Db::name('wxapp_log')->where('wxapp_id',$this->wxapp_id)->find();
            if(empty($wxapp)){  //如果为空，第一次创建
                Db::name('wxapp_log')->insert([
                    'wxapp_id'=>$this->wxapp_id,
                    'today_login'=>1,
                    'total_login'=>1,
                    'total_user'=>1
                ]);
            }else{  
                //否则，先检查今天是否有人登录
                $check = Db::name('login_log')->where('wxapp_id',$this->wxapp_id)->where('login_time','>',strtotime(date('Y-m-d 00:00:00')))->find();
                if(!empty($check)){   //今天有人登录，今日登录人数+1
                    Db::name('wxapp_log')->where('wxapp_id',$this->wxapp_id)->update([
                        'today_login'=>$wxapp['today_login'] + 1,
                        'total_login'=>$wxapp['total_login'] + 1,
                        'total_user'=>$wxapp['total_user'] + $is_reg
                    ]);
                }else{
                    Db::name('wxapp_log')->where('wxapp_id',$this->wxapp_id)->update([
                        'today_login'=>1,
                        'total_login'=>$wxapp['total_login'] + 1,
                        'total_user'=>$wxapp['total_user'] + $is_reg
                    ]);
                }
            }
            Db::name('login_log')->insert([
                'wxapp_id'=>$this->wxapp_id,
                'user_id'=>$user_id,
                'login_ip'=>$ip,
                'login_time'=>time(),
                'login_time'=>time()
            ]);

            $expire_time = time()+1800;
            $tokenArr = [
                'user_id'=>$user_id,
                'openid'=>$openid,
                'session_key'=>$session_key,
                'mobile'=>empty($user)?'':$user['mobile'],
                'nickname'=>empty($user)?'':$user['nickname'],
                'headimgurl'=>empty($user)?'':$user['headimgurl'],
                'expire_time'=>$expire_time
            ];
            $token = md5(uniqid().uniqid()).md5(time().$user_id);
            \Cache::set($token,json_encode($tokenArr),1800);
            $this->returnJson(10001,'登录成功',['token'=>$token,'userinfo'=>$tokenArr],$data);
        }else{
            $this->returnJson(-10001,'登录失败',[],$data);
        }
    }

    //授权获取手机号
    public function getMobile()
    {

    }
}